<template>
	<div class="system-area-container">
		<el-card shadow="hover" class="mb10" v-auth="'system:area:list'">
			<el-input size="small" v-model="search.areaName" placeholder="请输入区划名称" prefix-icon="el-icon-search" style="max-width: 180px;" class="ml10"></el-input>
			<el-select size="small" v-model="search.areaLevel" placeholder="请选择区划等级" style="max-width: 180px;" class="ml10">
				<el-option label="省级" :value="1"></el-option>
				<el-option label="地级" :value="2"></el-option>
				<el-option label="县级" :value="3"></el-option>
				<el-option label="乡级" :value="4"></el-option>
				<el-option label="村级" :value="5"></el-option>
			</el-select>
			<el-button size="small" icon="el-icon-search" type="primary" class="ml10" @click="onTableSearch()">查询</el-button>
			<el-button size="small" icon="el-icon-refresh" @click="onResetSearch()">重置</el-button>
		</el-card>
		<el-card shadow="hover">
			<el-button v-auth="'system:area:add'" size="small" type="primary" class="ml10" icon="el-icon-document-add" @click="onOpenAddArea()">新增</el-button>
			<el-table ref="tableAreaRef" stripe style="width: 100%" row-key="id" 
				:data="tableData.data"
				:lazy="true" 
				:load="onLoadTableData" 
				:tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
				<el-table-column label="区划代码" prop="id" show-overflow-tooltip></el-table-column>
				<el-table-column label="区划名称" prop="areaName" show-overflow-tooltip></el-table-column>
				<el-table-column label="省会城市" prop="capitalCity" show-overflow-tooltip></el-table-column>
				<el-table-column label="区划简称" prop="shortName" width="70" align="center" show-overflow-tooltip></el-table-column>
				<el-table-column label="区划等级" width="70" align="center" show-overflow-tooltip>
					<template #default="scope">
						<span v-if="scope.row.areaLevel == 1" class="color-primary">省级</span>
						<span v-if="scope.row.areaLevel == 2" class="color-warning">地级</span>
						<span v-if="scope.row.areaLevel == 3" class="color-success">县级</span>
						<span v-if="scope.row.areaLevel == 4" class="color-danger">乡级</span>
						<span v-if="scope.row.areaLevel == 5" class="color-info">村级</span>
					</template>
				</el-table-column>
				<el-table-column label="邮政编码" prop="postCode" width="100" show-overflow-tooltip></el-table-column>
				<el-table-column label="备注" prop="remark" show-overflow-tooltip></el-table-column>
				<el-table-column label="状态" show-overflow-tooltip width="70">
					<template #default="scope">
						<span v-if="scope.row.status == '0'" class="color-primary">正常</span>
						<span v-else-if="scope.row.status == '1'" class="color-info">停用</span>
						<span v-else class="color-danger">删除</span>
					</template>
				</el-table-column>
				<el-table-column label="区划排序" prop="orderNum" width="80" align="center" show-overflow-tooltip></el-table-column>
				<!-- <el-table-column label="创建时间" prop="createTime" width="150" show-overflow-tooltip></el-table-column> -->
				<el-table-column label="操作" show-overflow-tooltip width="125" 
					v-auths="['system:area:add', 'system:area:edit', 'system:area:remove']">
					<template #default="scope">
						<el-button v-auth="'system:area:add'" size="mini" type="text" @click="onOpenAddArea(scope.row)">新增</el-button>
						<el-button v-auth="'system:area:edit'" size="mini" type="text" @click="onOpenEditArea(scope.row)">修改</el-button>
						<el-button v-auth="'system:area:remove'" size="mini" type="text" @click="onTabelRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<AddArea ref="addAreaRef" @submit="onTableSearch"/>
		<EditArea ref="editAreaRef" @submit="onTableSearch"/>
	</div>
</template>

<script lang="ts">
import { ref, toRefs, reactive, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import AddArea from '/@/views/system/area/addArea.vue';
import EditArea from '/@/views/system/area/editArea.vue';
import { getAreaList, delArea } from '/@/api/system/area/index';
export default {
	name: 'area',
	components: { AddArea, EditArea },
	setup() {
		const tableAreaRef = ref();
		const addAreaRef = ref();
		const editAreaRef = ref();
		const rootId = '100000000000';
		const state = reactive({
			search:{
				areaName: '',
				areaLevel: 1,
			},
			tableData: {
				data: [],
				total: 0,
				loading: false
			}
		});

		const loadTableData = (params) => {
			getAreaList(params).then((res) => {
				if(res.code == 0){
					state.tableData.data = res.data;
					state.tableData.total = res.total;
				}
			});
		}
		
		const onLoadTableData = (row, treeNode, resolve) => {
			getAreaList({parentId: row.id}).then((res) => {
				if(res.code == 0){
					resolve(res.data);
				}
			});
		}

		//查询
		const onTableSearch = () => {
			if(state.search.areaName){
				loadTableData(state.search);
			}else{
				loadTableData({areaLevel: 1});
			}
		}
		//重置
		const onResetSearch = () => {
			state.search.areaName = undefined;
			loadTableData({areaLevel: 1});
		}
		// 打开新增区划弹窗
		const onOpenAddArea = (row: object) => {
			addAreaRef.value.openDialog(row);
		};
		// 打开编辑区划弹窗
		const onOpenEditArea = (row: object) => {
			editAreaRef.value.openDialog(row);
		};
		// 删除当前行
		const onTabelRowDel = (row: object) => {
			ElMessageBox.confirm('确定删除该条【' + row.areaName + '】区划信息吗？', '提示', {
				confirmButtonText: '删除',
				cancelButtonText: '取消',
				type: 'warning',
			}).then(() => {
				delArea([row.id]).then((res) => {
					if(res.code === 0){
						ElMessage.success('删除成功');
						onTableSearch();
					}
				});
			}).catch(() => {});
		};
		// 页面加载时
		onMounted(() => {
			loadTableData({areaLevel: 1});
		});
		return {
			tableAreaRef,
			addAreaRef,
			editAreaRef,
			onTableSearch,
			onResetSearch,
			onLoadTableData,
			onOpenAddArea,
			onOpenEditArea,
			onTabelRowDel,
			...toRefs(state),
		};
	},
};
</script>
<style>
.el-table .primary-row {
	--el-table-tr-bg-color: var(--el-color-primary-light-1);
}
.el-table .warning-row {
	--el-table-tr-bg-color: var(--el-color-warning-light-1);
}
.el-table .success-row {
	--el-table-tr-bg-color: var(--el-color-success-light-1);
}
.el-table .danger-row {
	--el-table-tr-bg-color: var(--el-color-danger-light-1);
}
</style>